//
//  ShopTableViewController.swift
//  quizDemo
//
//  Created by LiuXing on 2017/9/10.
//  Copyright © 2017年 quiz. All rights reserved.
//

import UIKit

private let reuseIdentifier1 = "ShopTableCell"
class ShopTableViewController: UITableViewController {
    
    var item: [String]! = ["RPG",
                           "ACTION",
                           "BORD",
                           "DICE",
                           "SHOTTING",
                           "WORD",
                           "SPORT",
                           "RACEING"
                           ]
    
    var data: [ShopTableModel]? = [ShopTableModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        creatData()
        tableView.rowHeight = 44
        tableView.register(UINib.init(nibName: "ShopTableCell", bundle: nil), forCellReuseIdentifier: reuseIdentifier1)
    }
    
    func creatData() {
        for i in 0 ..< item.count {
            let model = ShopTableModel()
            model.title = item[i]
            data?.append(model)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableView.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data!.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier1, for: indexPath) as! ShopTableCell
        let model = data?[indexPath.row]
        cell.contentLabel?.text = model?.title
        if (model?.selct)! {
            cell.contentView.backgroundColor = UIColor.lightGray
        } else {
            cell.contentView.backgroundColor = UIColor.white
        }
        let red = CGFloat(arc4random()%256)/255.0
        let green = CGFloat(arc4random()%256)/255.0
        let blue = CGFloat(arc4random()%256)/255.0
        cell.leftView.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let model = data?[indexPath.row]
        model?.selct = true
        let vc = ShopDetailViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
}
